<!--
  - Tencent is pleased to support the open source community by making BK-ITSM 蓝鲸流程服务 available.
  - Copyright (C) 2021 THL A29 Limited, a Tencent company.  All rights reserved.
  - BK-ITSM 蓝鲸流程服务 is licensed under the MIT License.
  -
  - License for BK-ITSM 蓝鲸流程服务:
  - -------------------------------------------------------------------
  -
  - Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  - documentation files (the "Software"), to deal in the Software without restriction, including without limitation
  - the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
  - and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  - The above copyright notice and this permission notice shall be included in all copies or substantial
  - portions of the Software.
  -
  - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
  - LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  - NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  - WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  - SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
  -->

<template>
    <div v-if="item.showFeild">
        <bk-form-item :label="item.name" :required="item.validate_type === 'REQUIRE'" :desc="item.tips" desc-type="icon">
            <bk-select :class="{ 'bk-border-error': item.checkValue }"
                :disabled="(item.is_readonly && !isCurrent) || disabled"
                searchable
                multiple
                :font-size="'medium'"
                :placeholder="item.desc"
                v-model="selectedItems"
                @selected="selected"
                @toggle="item.checkValue = false">
                <bk-option v-for="option in options"
                    :key="option.key"
                    :id="option.key"
                    :name="option.name">
                </bk-option>
            </bk-select>
            <template v-if="item.checkValue">
                <p class="bk-task-error" v-if="item.checkMessage">{{ item.checkMessage }}</p>
                <p class="bk-task-error" v-else>{{ item.name }}{{$t('m.newCommon["为必填项！"]')}}</p>
            </template>
        </bk-form-item>
    </div>
</template>

<script>
    import mixins from '../../commonMix/field.js'
    export default {
        name: 'MULTISELECT',
        mixins: [mixins],
        props: {
            item: {
                type: Object,
                required: true,
                default: () => {
                }
            },
            fields: {
                type: Array,
                default () {
                    return []
                }
            },
            isCurrent: {
                type: Boolean,
                default: false
            },
            disabled: {
                type: Boolean,
                default: false
            }
        },
        data () {
            return {
                options: [],
                selectedItems: []
            }
        },
        watch: {
            'item.val' (newVal, oldVal) {
                this.selectedItems = this.item.val === '' ? [] : this.item.val.split(',')
                this.conditionField(this.item, this.fields)
            },
            'item.choice' (newVal, oldVal) {
                if ((this.item.source_type === 'API' || this.item.source_type === 'DATADICT' || this.item.source_type === 'RPC') && (oldVal.length !== newVal.length)) {
                    this.getOption()
                }
            }
        },
        async mounted () {
            if (this.item.value && !this.item.val) {
                this.item.val = this.item.value
            }
            await this.getOption()
            const valueStatus = await this.judgeValue(this.item.val, this.item.choice)
            this.item.val = valueStatus ? this.item.val : ''
            this.selectedItems = this.item.val === '' ? [] : this.item.val.split(',')
            this.conditionField(this.item, this.fields)
        },
        methods: {
            async getOption () {
                this.options = this.item.choice = await this.getFieldOptions(this.item)
            },
            selected (ids) {
                this.item.val = ids.join(',')
                if (this.item.related_fields && this.item.related_fields.be_relied) {
                    this.item.related_fields.be_relied.forEach(ite => {
                        this.fields.forEach(it => {
                            if (ite === it.key) {
                                it.val = it.value = ''
                            }
                        })
                    })
                }
            }
        }
    }
</script>

<style lang='scss' scoped>

</style>
